package jpg1980.gttunes.dao.mysql;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;

import jpg1980.gttunes.beans.Car;
import jpg1980.gttunes.beans.DriveTrain;
import jpg1980.gttunes.beans.Manufacturer;
import jpg1980.gttunes.beans.TuneBasic;
import jpg1980.gttunes.beans.Tuner;
import jpg1980.gttunes.dao.AbstractDao;
import jpg1980.gttunes.dao.CarDao;
import jpg1980.gttunes.dao.ManufacturerDao;

public class CarDaoMySqlImpl extends AbstractDao implements CarDao
{
	
	static Logger logger = Logger.getLogger(CarDaoMySqlImpl.class);

	@Override
	public int create(Car car)
	{
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void delete(int id)
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public Car read(int id)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Car> readAll()
	{
		List<Car> carList = new ArrayList<Car>();
		
		PreparedStatement readAllStatement;
		
		try
		{
			Connection conn = getConnection();

			logger.debug("");
			readAllStatement = conn.prepareStatement("SELECT * from CAR GROUP BY carModelName");
			
			ResultSet rs = readAllStatement.executeQuery();
			
			while (rs.next())
			{
				Manufacturer manufacturer = new Manufacturer(rs.getString("manufacturerName"));
				
				DriveTrain driveTrain = new DriveTrain(rs.getString("driveTrainName"));
				
				Car car = new Car(	rs.getString("carModelName"),
									driveTrain,
									manufacturer);
				
				carList.add(car);
			}
		}
		catch (SQLException e)
		{
			super.cleanUp();
		}
		finally
		{
			super.cleanUp();
		}
		
		return carList;
	}

	@Override
	public List<Car> readSelection(int[] ids)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void update(Car car)
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<Car> readByManufacturer(Manufacturer aManufacturer)
	{
		List<Car> carList = new ArrayList<Car>();
		
		PreparedStatement readByManufacturerStatement;
		
		try
		{
			Connection conn = getConnection();

			logger.debug("");
			readByManufacturerStatement = conn.prepareStatement("SELECT * from CAR WHERE manufacturerName = ? GROUP BY carModelName");
			readByManufacturerStatement.setString(1, aManufacturer.getName());
			ResultSet rs = readByManufacturerStatement.executeQuery();
			
			while (rs.next())
			{
				Manufacturer manufacturer = new Manufacturer(rs.getString("manufacturerName"));
				
				DriveTrain driveTrain = new DriveTrain(rs.getString("driveTrainName"));
				
				Car car = new Car(	rs.getString("carModelName"),
									driveTrain,
									manufacturer);
				
				carList.add(car);
			}
		}
		catch (SQLException e)
		{
			super.cleanUp();
		}
		finally
		{
			super.cleanUp();
		}
		
		return carList;
	}
	

}
